package com.ghost.view.pop.pack
{
	import com.ghost.UI.Ghost_Btn;
	import com.ghost.UI.Ghost_tip;
	import com.ghost.manager.Pack_manager;
	import com.ghost.manager.Tip_manager;
	import com.ghost.model.Configure_url;
	import com.ghost.model.Global_data;
	import com.ghost.model.Object_pool;
	import com.ghost.utils.MovieClipUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.setTimeout;

	/**
	 * 存放物品的背包cell （包括物品类型 物品id通过id可以知道是否可以交易 丢弃 等等）
	 * @author ghost
	 */	
	public class Pack_cell extends Sprite
	{
		public var data_num:int=-1;//data_v里的索引
		public var cell_num:int;//cell在pack_cell_v里的序号
		private var type:uint;//物品类型
		private var id:String='0';//物品id
		private var color_n:int=0;//颜色序号
		private var sale_bool:Boolean;//是否可以交易
		private var giveup_bool:Boolean;//是否可以放弃
		
		private var bg_bm:Bitmap;
		private var click_bool:Boolean;//是否已经点击选中
		private var content_sprite:Sprite;//cell里包含的物品容器
		private var img_ld:Loader;//图片loader
		private var first_drag_p:Point;//拖动范围起始点
		private var num_txt:TextField;//个数文本
		private var num:int=1;//物品个数
		private var icon_bm:Bitmap;//物品图标
		public function Pack_cell()
		{
			var bg_bm_c:* = Object_pool.loaded_obj_pool.get("pack_ui").get("cell_bg");
			bg_bm = new Bitmap(bg_bm_c);
			this.addChild(bg_bm);
			this.buttonMode = true;
			content_sprite = new Sprite();
			content_sprite.name="content_sprite";
			this.addChild(content_sprite);
			//this.addEventListener(MouseEvent.CLICK,cHandler);
			this.addEventListener(MouseEvent.MOUSE_OVER,over_Handler);
			this.addEventListener(MouseEvent.MOUSE_OUT,out_Handler);
		}
		/**
		 * 添加物品 
		 * @param id 物品id
		 * @param type 物品类型
		 * @param num 物品个数
		 */		
		public function add(id1:String,type1:uint,num1:int=1,color_n1:int=0):void
		{
			num = num1;
			id = id1;
			type = type1;
			color_n = color_n1;
			remove();
			if(Object_pool.tip_obj_pool.containsKey(this))Ghost_tip(Object_pool.tip_obj_pool.get(this)).change_content(id,Global_data.equipment_v[type],color_n);
			else Tip_manager.getInstance().add_tip(this,id,Global_data.equipment_v[type],1,color_n);
			if(Object_pool.icon_pool.containsKey(id))
			{
				if(id!='0')content_sprite_addchild(new Bitmap(Object_pool.icon_pool.get(id)));
				else
				{
					var over_bm:Bitmap = new Bitmap(Object_pool.loaded_obj_pool.get("pack_ui").get("lock_over"));
					var out_bm:Bitmap = new Bitmap(Object_pool.loaded_obj_pool.get("pack_ui").get("lock_out"));
					var lock_btn:Ghost_Btn = new Ghost_Btn(new <Object>[out_bm,over_bm,over_bm,out_bm],lock_handler);
					lock_btn.setsize(46,46);
					content_sprite.addChild(lock_btn);
					Tip_manager.getInstance().add_tip(lock_btn,id);
				}
			}
			else
			{
				img_ld = new Loader();
				img_ld.load(new URLRequest(Configure_url.img_url+"icon/"+id+".png"));
				img_ld.contentLoaderInfo.addEventListener(Event.COMPLETE,img_cHandler);
			}
			if(id!='0')
			{
				content_sprite.addEventListener(MouseEvent.MOUSE_DOWN,down_Handler);
				content_sprite.addEventListener(MouseEvent.MOUSE_UP,up_Handler);
			}
		}
		private function down_Handler(e:MouseEvent):void
		{
			first_drag_p = this.globalToLocal(new Point(0,0));
			MovieClipUtil.swapChildToTop(this.parent,this);
			content_sprite.startDrag(false,new Rectangle(first_drag_p.x,first_drag_p.y,Global_data.stage_s_width,Global_data.stage_s_height));
			content_sprite.addEventListener(MouseEvent.MOUSE_OUT,content_out_Handler);
		}
		private function up_Handler(e:MouseEvent):void
		{
			if(content_sprite.x==0 && content_sprite.y==0)
			{
				if(Pack_manager.getInstance().current_cell!=null)
				{
					Pack_manager.getInstance().current_cell.remove_filters();
					Pack_manager.getInstance().current_cell.click_bool = false;
				}
				Pack_manager.getInstance().current_cell = this;
				bg_bm.filters = [new GlowFilter()];
				click_bool = true;
				content_sprite.stopDrag();
			}
			else
			{
				content_sprite.removeEventListener(MouseEvent.MOUSE_OUT,content_out_Handler);
				stop();
			}
		}
		private function content_out_Handler(e:MouseEvent):void
		{
			stop();
		}
		private function stop():void
		{
			content_sprite.stopDrag();
			if(content_sprite.dropTarget==null)trace('超出范围弹出扔掉物品对话框');
			else parse_drop_target(content_sprite.dropTarget.parent);
		}
		private var content_sprite_p:Point;
		private function parse_drop_target(target:DisplayObject):void
		{
			content_sprite_p = this.localToGlobal(new Point(content_sprite.x,content_sprite.y));
			if(target!=null && target.parent!=null)
			{
				if(target is Pack_cell && target.parent is Pack_content)
				{
					trace('cell是空的');
					target_add(target);
				}
				else if(target.name=="content_sprite" && target.parent is Pack_cell)
				{
					trace('cell里有东西');
					target_change(target.parent);
				}
				else if(content_sprite_p.x<this.parent.parent.x-content_sprite.width || content_sprite_p.y<this.parent.parent.y-content_sprite.height || content_sprite_p.x>this.parent.parent.x+Pack_manager.getInstance().pack_main.w || content_sprite_p.y>this.parent.parent.y+Pack_manager.getInstance().pack_main.h)
				{
					trace('超出范围弹出扔掉物品对话框');
				}
				else
				{
					back();
				}
			}
		}
		//cell是空的
		private function target_add(target:DisplayObject):void
		{
			if(Pack_cell(target).name != this.name)
			{
				Pack_cell(target).add(id,type,num,color_n);
				Pack_cell(target).data_num = data_num;
				Pack_manager.getInstance().change_data(Pack_cell(target));
				remove();
				first();
			}
			back();
		}
		//cell里有东西
		private function target_change(target:DisplayObject):void
		{
			if(Pack_cell(target).id==id && Pack_cell(target).color_n==color_n && Pack_cell(target).type==type)
			{
				Pack_cell(target).num = Pack_cell(target).num+num;
				Pack_cell(target).show_txt();
				Pack_manager.getInstance().change_data(Pack_cell(target));
				remove();
				first();
			}
			else
			{
				var id_c:String = id;
				var type_c:int = type;
				var num_c:int = num;
				var color_n_c:int = color_n;
				var data_num_c:int = data_num;
				add(Pack_cell(target).id,Pack_cell(target).type,Pack_cell(target).num,Pack_cell(target).color_n);
				data_num = Pack_cell(target).data_num;
				Pack_cell(target).add(id_c,type_c,num_c,color_n_c);
				Pack_cell(target).data_num = data_num_c;
				Pack_manager.getInstance().change_data(target,Pack_cell(target));
			}
			back();
		}
		/**
		 * 状态初始化
		 */		
		public function first():void
		{
			remove_filters();
			//id = '0';
			//type = 0;
			data_num = -1;
			sale_bool = false;
			giveup_bool = false;
			click_bool = false;
			//num = 1;
			Ghost_tip(Object_pool.tip_obj_pool.get(this)).change_content("0");
		}
		//回到初始位置
		private function back():void
		{
			content_sprite.x = 0;
			content_sprite.y = 0;
		}
		private function lock_handler(e:MouseEvent):void
		{
			trace('弹出解锁窗口!!!')
		}
		//icon 加载完成
		private function img_cHandler(e:Event):void
		{
			img_ld.contentLoaderInfo.removeEventListener(Event.COMPLETE,img_cHandler);
			icon_bm = Bitmap(img_ld.content);
			content_sprite_addchild(icon_bm);
			Object_pool.icon_pool.put(id,icon_bm.bitmapData);
		}
		private function content_sprite_addchild(bm:Bitmap):void
		{
			content_sprite.addChild(bm);
			if(num_txt==null)num_txt = new TextField();
			show_txt();
			num_txt.selectable = false;
			num_txt.x = content_sprite.width-num_txt.width;
			num_txt.y = content_sprite.height-num_txt.height;
			content_sprite.addChild(num_txt);
			click_bool = false;
		}
		/**
		 * 改变数量
		 */		
		public function show_txt():void
		{
			num_txt.text=String(num);
			num_txt.setTextFormat(new TextFormat(null,10,0xffffff));
			num_txt.width=num_txt.textWidth+4;
			num_txt.height = num_txt.textHeight+2;
			num_txt.filters=[new GlowFilter()];
		}
		//移上
		private function over_Handler(e:MouseEvent):void
		{
			if(!click_bool)bg_bm.filters = [new GlowFilter()];
		}
		//移出
		private function out_Handler(e:MouseEvent):void
		{
			if(!click_bool)remove_filters();
		}
		private function remove_filters():void
		{
			bg_bm.filters=[];
		}
		/**
		 * 移除content_sprite里的所有对象
		 */		
		public function remove():void
		{
			MovieClipUtil.removeAllChild(content_sprite);
		}
	}
}